import {
  Controller,
  Get,
  Post,
  Body,
  Req,
  Res,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Put,
  Param,
  Query,
} from '@nestjs/common';

import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { Request, Response, query } from 'express';
import { UploadService } from './upload.service';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { ApiTags } from '@nestjs/swagger';
import { Upload } from './entities/upload.entity';
import fs, {
  createWriteStream,
  createReadStream,
  WriteStream,
  readFileSync,
  mkdir,
  mkdirSync,
} from 'fs';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import path, { extname, join } from 'path';
// const { Buffer } = require('buffer')
import { Buffer } from 'buffer';
interface IUploaddBody {
  name: string;
  notename: string;
  mybase: string;
}
@ApiTags('上传图片')
@Controller('upload')
export class UploadController {
  constructor(private readonly uploadService: UploadService) {}
  @Post()
  // @UseInterceptors(FileInterceptor('file'))
  test(@Req() req: Request, @Res() res: Response) {
    console.log(req.body);
    // req.on('data', (reqs) => {
    //   console.log(reqs);
    // });
    res.send('22');
    // return req;
  }
  @Get('album')
  async gettest(@Query() query) {
    const message = await this.uploadService.getdiskStorage(
      join(__dirname, `../images/${query.hash}`),
    );
    // console.log(message);
    return message;
  }

  @Post('album')
  @UseInterceptors(FileInterceptor('file'))
  upload(@UploadedFile() file, @Body() Body) {
    // console.log(file, '---', Body);
    const writeImage = createWriteStream(
      join(__dirname, `../images/${Body.myfilename}`, `${Body.filename}`),
    );
    writeImage.write(file.buffer);

    return '上传成功';
  }
  // @Put('album')
  // @UseInterceptors(FileInterceptor('file'))
  // uploaad(@UploadedFile() file) {
  //   console.log(file);
  //   return true;
  // }
  // @Get('export')
  // downLoad(@Res() res: Response) {
  //   const url = join(__dirname, 'yui.png');
  //   // res
  //   console.log(url);
  //   res.send(url);
  //   // return  true
  // }
  @Post('merge')
  meage(@Body() body) {
    // console.log(body.hash);
    const bufferlist = [];
    for (let i = 0; i < body.count; i++) {
      const yui = readFileSync(
        join(__dirname, `../images/${body.hash}/${body.hash}_${i + 1}`),
      );
      bufferlist.push(yui);
    }
    const buffer = Buffer.concat(bufferlist);
    const ws = createWriteStream(
      join(__dirname, `../images`, `${body.hash}.png`),
    );
    ws.write(buffer);
  }
  @Put('album')
  async uploaad(@Body() body: IUploaddBody) {
    // console.log(body.name);
    await this.uploadService.saveImg(body);
    // createUploadDto.on('file', (res) => {
    //   console.log(res);
    // });
    return true;
  }
  // @Get(['export', ':yui'])
  @Get('export')
  async downLoad(@Query() query: any) {
    console.log('query.name, query.note', query.name, query.notename);
    const data = await this.uploadService.getImg(query.name, query.notename);
    // console.log(data);
    return data;
  }

  @Get('xx:yui')
  async downLoads() {
    console.log(1234);
    return true;
  }
}
